import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/components/home/Home'
import dasbord from '@/components/Dasbord'
import Login from '@/components/login/login'
import index from '@/components/common/Index'
Vue.use(Router)
const router = new Router({
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: Login
    },
    {
      path: '/index',
      name: '首页',
      meta:{
        requireAuth: true
      },
      component: index,
      children: [
        {
          path: 'home',
          name: 'home',
          component: Home
        },
        {
          path: 'dasbord',
          name: 'dasbord',
          component: dasbord
        }
      ]
    },
    {
      path: '/',
      redirect: '/login'
    }

  ],

});


router.beforeEach((to, from, next) => {
  if (to.matched.some(res => res.meta.requireAuth)) {
    if (localStorage.getItem('userInfo')) {
      next()
    } else {// 没登录则跳转到登录界面
      next({
        path: '/login',
        query: {redirect: to.fullPath}
      })
    }
  } else {
    next()
  }
});

export default router;
